package com.leather.question.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.common.entity.Answer;
import com.leather.common.entity.Question;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.BeanKit;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.vo.MessageVo;
import com.leather.question.config.MQConfig;
import com.leather.question.mapper.AnswerMapper;
import com.leather.question.mapper.QuestionMapper;
import com.leather.question.service.AnswerService;
import com.leather.question.vo.AnswerAddVo;
import com.leather.question.vo.AnswerDetailQueryVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {


    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Override
    public Result addAnswer(AnswerAddVo answerAddVo) {
        Result result = answerAddVo.validate();
        if (!result.isSuccess()) {
            return result;
        }
        Answer answer = (Answer) result.getData();
        //判断问题还在不在
        Question question = questionMapper.selectById(answer.getQuestionId());
        CheckKit.check(question != null, CodeMsg.QUESTION_DELETED);
        //判断当前用户是否已经评论了
        int count = query().eq("user_id", answer.getUserId()).eq("question_id", answer.getQuestionId()).count();
        CheckKit.check(count == 0, CodeMsg.DUPPY_ANSWER);
        CheckKit.check(save(answer), CodeMsg.SERVER_ERROR);
        amqpTemplate.convertAndSend(MQConfig.ANSWERS_QUEUE, "1-" + answer.getQuestionId());
        MessageVo messageVo = new MessageVo();
        messageVo.setMessageType(0);
        messageVo.setMessageTitle("[问答通知]");
        messageVo.setMessageContent("你的问题有人回答了，点击查看详情");
        messageVo.setMessageUrl("/pages/question/questiondetail?questionId=" + answer.getQuestionId());
        messageVo.setUserId(question.getUserId());
        amqpTemplate.convertAndSend(MQConfig.MESSAGE_QUENE, BeanKit.beanToString(messageVo));
        return Result.success("ok");
    }

    @Override
    public Result getByquestionId(int questionId, int pageId) {
        CheckKit.check(questionId > 0, CodeMsg.ERROR_QUESTION_ID);
        return Result.success(answerMapper.getByQuestionId(questionId, pageId));
    }

    @Override
    public Result deleteAnswer(int answerId, int userId) {
        CheckKit.check(answerId > 0, CodeMsg.ERROR_ANSWER_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        Answer answer = getById(answerId);
        int count = answerMapper.deleteAnswer(answerId, userId);
        CheckKit.check(count > 0, CodeMsg.SERVER_ERROR);
        amqpTemplate.convertAndSend(MQConfig.ANSWERS_QUEUE, "2-" + answer.getQuestionId());
        return Result.success("ok");
    }

    @Override
    public Result getUserAnswer(int userId, int pageId) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        List<AnswerDetailQueryVo> lists = answerMapper.getUserAnswer(userId, pageId);
        return Result.success(lists);
    }
}
